#!/usr/local/env ruby

########################################

PROJECTDIR = File.dirname(File.dirname(File.expand_path(__FILE__)))
SANDBOXDIR = File.join(PROJECTDIR, 'sandbox')
DUPDETECTOR = File.join(PROJECTDIR, 'source', 'dup_detector.rb')

class Sandbox
  def initialize(file_data)
    @paths = []
    file_data.each do | fname, contents |
      path = File.join(SANDBOXDIR, fname)
      File.open(path, 'w') { |f| f.puts contents }
      @paths << path
    end
  end
  
  def raze
    @paths.each { |path| File.delete(path) }
  end
  
  def self.run(file_data, &block)
    sandbox = Sandbox.new(file_data)
    yield
    sandbox.raze
  end
end

### 2a #####################################

data = {
  'fileA'=>'This is a test',
  'fileB'=>'This is a test',
  'fileC'=>'The quick brown fox',
  'fileD'=>'',
}

Sandbox.run(data) do
  puts "#$/*** Case 2a ***"
  1.upto(5) do |i|
    puts "#$/* match length #{i} *"
    puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
  end
end

### 2b #####################################

data = {
  'fileA'=>'kjskj adlfdsfk This is a test  dslkdsk ',
  'fileB'=>'zsjdkjs This is a test dsldkjklds',
  'fileC'=>'The quick brown fox',
}

Sandbox.run(data) do
  puts "#$/*** Case 2b ***"
  1.upto(5) do |i|
    puts "#$/* match length #{i} *"
    puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
  end
end

### 2c #####################################

data = {
  'fileA'=>'kjskj adlfdsfk This is a test  dslkdsk  This is a test  sdkldsflks',
  'fileC'=>'The quick brown fox',
}

Sandbox.run(data) do
  puts "#$/*** Case 2c ***"
  1.upto(5) do |i|
    puts "#$/* match length #{i} *"
    puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
  end
end

### 2d #####################################

data = {
  'fileA'=>'This is a test',
  'fileC'=>'This is dslkdsk a test',
}

Sandbox.run(data) do
  puts "#$/*** Case 2d ***"
  1.upto(4) do |i|
    puts "#$/* match length #{i} *"
    puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
  end
end               

### 2e #####################################

data = {
  'fileA'=>'This is a test for the quick brown fox',
  'fileB'=>'This is a test',
  'fileC'=>'The quick brown fox',
}

Sandbox.run(data) do
  i = 4
  puts "#$/*** Case 2e ***"
  puts "#$/* match length #{i} *"
  puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
end

### 3 #####################################

data = {
  'fileA'=>'This is a test',
  'fileB'=>'This is a test',
  'fileC'=>'This is a test',
}

Sandbox.run(data) do
  puts "#$/*** Case 3 ***"
  4.upto(5) do |i|
  puts "#$/* match length #{i} *"
  puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
  end
end

### 4 #####################################

data = {
  'fileA'=>'This is a test sentence here.',
  'fileB'=>'is a test sentence here',
  'fileC'=>'a test sentence here and there.',
}

Sandbox.run(data) do
  puts "#$/*** Case 4 ***"
  [3, 6].each do |i|
  puts "#$/* match length #{i} *"
  puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
  end
end

### 5 #####################################

data = {
  'fileA'=>'This is test sentence A.',
  'fileB'=>'This is test sentence B.',
  'fileC'=>'This is test sentence C.',
  'fileD'=>'Another group of words D.',
  'fileE'=>'Another group of words E.',
  'fileF'=>'Another group of words F.',
}

Sandbox.run(data) do
  i = 4
  puts "#$/*** Case 5 ***"
  puts "#$/* match length #{i} *"
  puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
end

### 6 ###################################

data = {
  'fileA'=>'This is test sentence A.   Here is another match later in the document.',
  'fileB'=>'This is test sentence B.',
  'fileC'=>'This is test sentence C.  We can see another match later in the document to be found.',
  'fileD'=>'An initial prefix to the document that does not match,  Then match later in the document is here.',
}

Sandbox.run(data) do
  i = 4
  puts "#$/*** Case 6 ***"
  puts "#$/* match length #{i} *"
  puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
end

### 7 ###################################

data = {
  'fileA'=>'This is test sentence A.   Here is another match later in the document.',
  'fileB'=>'This is test sentence B.  Then match later in the document is here.',
  'fileC'=>'This is test sentence C.  We can see another match later in the document to be found.',
}

Sandbox.run(data) do
  i = 4
  puts "#$/*** Case 7 ***"
  puts "#$/* match length #{i} *"
  puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
end

### 8 ###################################

data = {
  'fileA'=>'This is a test sentence here.',
  'fileB'=>'is a test sentence here',
  'fileC'=>'a test sentence here and there.',
  'fileD'=>'is a test sentence that can be completed.',
  'fileE'=>'a test sentence for me.',
}

Sandbox.run(data) do
  i = 3
  puts "#$/*** Case 8 ***"
  puts "#$/* match length #{i} *"
  puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
end

### 9 ###################################

data = {
  'fileA'=>'This is a test for the quick brown fox',
  'fileB'=>'This is a test for the',
  'fileC'=>'This is a test for the quick',
}

Sandbox.run(data) do
  i = 4
  puts "#$/*** Case 9 ***"
  puts "#$/* match length #{i} *"
  puts `jruby #{DUPDETECTOR} -i #{i} #{SANDBOXDIR}`
end
